//
//  PostWindow.m
//  GoldBook
//
//  Created by wmz on 2021/8/17.
//  Copyright © 2021 wmz. All rights reserved.
//

#import "PostWindow.h"
#import "WebViewController.h"
//#import "LogonViewController.h"
#import "PostNaviVC.h"
#import "QuDanMainTabBarController.h"
static PostWindow* postManage = nil;
@interface PostWindow()
@property(nonatomic,copy)PostResultImage imageBlock;

@end
@implementation PostWindow
//单例
+ (instancetype)shareInstance
{
    static dispatch_once_t onceToken ;
    dispatch_once(&onceToken, ^{
        postManage = [[super allocWithZone:NULL] init];
    }) ;
    return postManage ;
}

+ (void)windowPushToVC:(nullable UIViewController*)VC currentVC:(nullable UIViewController*)current{
    [PostWindow windowPushToVC:VC currentVC:current hideBottom:NO];
}

+ (void)windowPushToVC:(nullable UIViewController*)VC currentVC:(nullable UIViewController*)current hideBottom:(BOOL)hide{
    if (!VC) return;;
    if (!current) current = [BaseAlert getCurrentVC];
    if (hide) VC.hidesBottomBarWhenPushed = YES;
    if ([VC isKindOfClass:NSString.class]) {
        VC = NSClassFromString((NSString*)VC).new;
    }
    
    [current.navigationController pushViewController:VC animated:YES];
}

+ (void)windowPresentToVC:(nullable UIViewController*)VC currentVC:(nullable UIViewController*)current{
    if (!VC) return;;
    if (!current) {
        current = [BaseAlert getCurrentVC];
    }
    [current presentViewController:VC animated:YES completion:nil];
}

+ (void)windowPresentToVC:(nullable UIViewController*)VC{
    [PostWindow windowPresentToVC:VC currentVC:nil];
}

+ (void)windowPushToVC:(nullable UIViewController*)VC{
    [PostWindow windowPushToVC:VC currentVC:nil hideBottom:NO];
}

+ (void)pushWeb:(NSString*)url title:(NSString*)title{
    WebViewController *vc = WebViewController.new;
    vc.url = url;
    vc.title = title;
    vc.hidesBottomBarWhenPushed = YES;
    [[BaseAlert getCurrentVC].navigationController pushViewController:vc animated:YES];
}

+ (void)pushHomeVC:(nullable UIViewController*)VC{
    UIApplication.sharedApplication.delegate.window.rootViewController = VC;
}

+ (void)pushLoginVC:(nullable NSString*)str{
    if (![[BaseAlert getCurrentVC] isKindOfClass:NSClassFromString(@"LogonViewController")]){
        [self performSelector:@selector(toLogonAction:) withObject:nil afterDelay:0.8];
    }
    if (str) PostInfoView(str, DialogWindow)
}

+ (void)toLogonAction:(BOOL)root{
    if (root){
        UIApplication.sharedApplication.delegate.window.rootViewController = QuDanMainTabBarController.new;
    }
    PostNaviVC *navi = [[PostNaviVC alloc]initWithRootViewController:NSClassFromString(@"QDLoginVC").new];
    navi.modalPresentationStyle = 0;
    [UIApplication.sharedApplication.delegate.window.rootViewController presentViewController:navi animated:YES completion:nil];
    
}


- (void)popRoot:(BOOL)root{
    [self performSelector:@selector(popAction:) withObject:@(root) afterDelay:0.8];
}

- (void)popAction:(NSNumber*)root{
    if (!root.boolValue) {
        [[BaseAlert getCurrentVC].navigationController popViewControllerAnimated:YES];
    }else{
        [[BaseAlert getCurrentVC].navigationController popToRootViewControllerAnimated:YES];
    }
}

- (void)pushImageVC:(PostResultImage)block maxCount:(NSInteger)maxCount{
    self.imageBlock = block;
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
    imagePickerVc.allowPreview = NO;
    imagePickerVc.minImagesCount = 1;
    imagePickerVc.maxImagesCount = maxCount;
    imagePickerVc.allowPickingVideo = NO;
    imagePickerVc.allowTakeVideo = NO;
    imagePickerVc.allowPickingImage = YES;
    imagePickerVc.allowTakePicture = YES;
    [[BaseAlert getCurrentVC] presentViewController:imagePickerVc animated:YES completion:nil];
}

- (void)pushImageVC:(PostResultImage)block{
    return [self pushImageVC:block maxCount:1];
}

- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto{
    
    if (self.imageBlock && photos.count) {
        self.imageBlock(photos);
    }
}

+ (id)allocWithZone:(struct _NSZone *)zone{
    return [PostWindow shareInstance] ;
}

- (id)copyWithZone:(struct _NSZone *)zone{
    return [PostWindow shareInstance] ;
}

@end
